#include "widget.h"

#include <QPushButton>
#include <QComboBox>
#include <QLabel>
#include <QTextBrowser>
#include <QFormLayout>
#include <QVBoxLayout>
#include <QProcess>
#include <QDesktopWidget>
#include <QApplication>
#include <QScreen>
#include <QDBusInterface>
#include <QVariant>
#include <QDir>
#include <QDateTime>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    getAllUser();
    initUI();
    updateDisplayUser();
}

Widget::~Widget()
{
}
void Widget::startSyncFiles()
{
    if (m_srcUserBox->currentText().isEmpty()) {
        return;
    }

    UserInfo info = m_srcUserBox->currentData().value<UserInfo>();

    m_syncBt->setEnabled(false);
    m_srcUserBox->setEditable(false);
    m_syncInfoBr->clear();

    QString user = qApp->property("User").toString();
    QProcess *process = new QProcess(this);
    QObject::connect(process, &QProcess::readyReadStandardOutput, this, [=]() { m_syncInfoBr->append(process->readAllStandardOutput()); });
    QObject::connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                     [=](int exitCode, QProcess::ExitStatus exitStatus) {
                         qInfo() << exitCode << exitStatus << process->readAllStandardError();
                         process->deleteLater();
                         m_syncBt->setEnabled(true);
                         m_srcUserBox->setEditable(true);
                     });

    //--exclude=** 过滤不同步的文件
    process->start("pkexec rsync -a -P --exclude=.Xauthority --chown=" + user + ":" + user + " " + info.second + "/ " + QDir::homePath());
}

void Widget::initUI()
{
    QSize size(480, 480);
    setMinimumSize(size);

    QLabel *srcUserLabel = new QLabel(this);
    srcUserLabel->setText("同步源用户");

    m_srcUserBox = new QComboBox(this);
    m_syncInfoBr = new QTextBrowser(this);
    m_syncBt = new QPushButton(this);
    m_syncBt->setText("同步");
    QObject::connect(m_syncBt, &QPushButton::clicked, this, &Widget::startSyncFiles);

    QFormLayout *formLayout = new QFormLayout;
    formLayout->addRow(srcUserLabel, m_srcUserBox);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addLayout(formLayout);
    layout->addWidget(m_syncInfoBr);
    layout->addWidget(m_syncBt, Qt::AlignCenter);

    setLayout(layout);

    move(QApplication::screens().first()->geometry().center() - QPoint(size.width() / 2, size.height() / 2));
}

void Widget::updateDisplayUser()
{
    m_srcUserBox->clear();
    for (int i = 0; i < m_allUserInfo.size(); i++) {
        QVariant userData;
        userData.setValue<UserInfo>(m_allUserInfo[i]);
        m_srcUserBox->addItem(m_allUserInfo[i].first, userData);
    }
    m_srcUserBox->setCurrentIndex(-1);
}

void Widget::getAllUser()
{
    m_allUserInfo.clear();
    QDBusInterface accountInter("com.deepin.daemon.Accounts", "/com/deepin/daemon/Accounts", "com.deepin.daemon.Accounts", QDBusConnection::systemBus());
    auto userDbusList = accountInter.property("UserList").toStringList();
    qInfo() << userDbusList;
    for (int i = 0; i < userDbusList.size(); i++) {
        if (userDbusList.at(i).isEmpty()) {
            continue;
        }

        QDBusInterface userInter("com.deepin.daemon.Accounts", userDbusList.at(i), "com.deepin.daemon.Accounts.User", QDBusConnection::systemBus());
        auto userName = userInter.property("UserName").toString();
        if (userName == qApp->property("User").toString()) {
            continue;
        }
        auto homeDir = userInter.property("HomeDir").toString();
        UserInfo userInfo;
        userInfo.first = userName;
        userInfo.second = homeDir;
        m_allUserInfo.append(userInfo);
    }
}
